package net.zhengxinyun.performance.controller.login;

import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.EvaluateUserEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.EvaluateUserFilter;
import net.zhengxinyun.performance.service.login.EvaluateUserService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/17.
 */
@RestController
@RequestMapping("/evaluateUser")
public class EvaluateUserController {

    private EvaluateUserService evaluateUserService;
    private TokenService tokenService;
    private LoginService loginService;

    @Autowired
    public EvaluateUserController(EvaluateUserService evaluateUserService, TokenService tokenService, LoginService loginService) {
        this.evaluateUserService = evaluateUserService;
        this.tokenService = tokenService;
        this.loginService = loginService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.insert(param.evaluateUserEntity);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.update(param.evaluateUserEntity);
            if (!"修改成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.delete(param.list);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            List<EvaluateUserFilter> list = evaluateUserService.query(param.evaluateUserFilter);
            if (list.size() == 0){
                param.evaluateUserFilter.setPageNumber(param.evaluateUserFilter.getPageNumber()-1);
                list = evaluateUserService.query(param.evaluateUserFilter);
            }
            Double pageTotal = Util.intGetTotalPage(evaluateUserService.queryForCount(param.evaluateUserFilter),param.evaluateUserFilter.getPageSize());
            HashMap<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对",null);
    }

    @PostMapping("/importEvaluateUser")
    public Result<String> importEvaluateUser(@RequestParam(value = "file")MultipartFile file){
        String str = evaluateUserService.importEvaluateUser(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }
}
class EvaluateUserParam{
    public EvaluateUserEntity evaluateUserEntity;
    public EvaluateUserFilter evaluateUserFilter;
    public TokenEntity tokenEntity;
    public List<String> list;
}
